/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.GUI;

import Data.ActivityLog.ActivityCrl;
import CrlException.CrlException;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import Data.ActivityLog.StatisticUtility;
import javax.swing.JOptionPane;

/**
 *
 * @author CUHUUHOANG
 */
public class GUIStatisticControl extends Thread {

    public static JLabel lblUrlQueued;
    public static JLabel lblUrlProcessed;
    public static JLabel lblThreadRunning;
    public static JLabel lblTime;
    public static JTextArea txtLog;
    boolean isSuspended;
    boolean isStopped;

    @Override
    public void run() {
        while (!isStopped) {
            try {
                checkTheadSuspend();
                lblUrlQueued.setText(StatisticUtility.getURLQueued() + "");
                lblUrlProcessed.setText(StatisticUtility.getURLProcessed() + "");
                lblThreadRunning.setText(StatisticUtility.getThreadRunning() + "");
                lblTime.setText(StatisticUtility.getTime());
                Thread.currentThread().sleep(350);
            } catch (Exception ex) {
                ActivityCrl.atiData.addAtivity(ex.getMessage());
            }
            try {
                txtLog.setText(StatisticUtility.getLog());
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error: ", JOptionPane.ERROR_MESSAGE);
            }


        }
    }

    public GUIStatisticControl(JLabel lblUrlQueued, JLabel lblUrlProcessed, JLabel lblThreadRunning, JLabel lblTime, JTextArea txtLog) {
        this.lblUrlQueued = lblUrlQueued;
        this.lblUrlProcessed = lblUrlProcessed;
        this.lblThreadRunning = lblThreadRunning;
        this.lblTime = lblTime;
        this.txtLog = txtLog;
        isSuspended = false;
        isStopped = false;
    }

    synchronized public void suspendWork() throws CrlException {
        try {
            if (isSuspended) {
                isSuspended = false;
            } else {
                isSuspended = true;
            }
            notifyAll();
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }
    }

    synchronized public void StopThead() throws CrlException {
        try {
            this.isStopped = true;
            this.isSuspended = false;
            notifyAll();
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }

    }

    synchronized public void checkTheadSuspend() throws CrlException {
        try {
            if (isSuspended) 
                    wait();
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        }
    }
}
